package com.haoshuang.zhouzhoubang.ui.page.welfare.insure.apply

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.gyf.immersionbar.ktx.immersionBar
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityInsureApplyBinding
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.haoshuang.zhouzhoubang.utils.MultiMediaChooseUtils
import com.haoshuang.zhouzhoubang.utils.OSSUploadUtils
import com.haoshuang.zhouzhoubang.utils.PermissionHelper
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.toJson
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical

/**
 *  保险申请页面
 */
class InsureApplyActivity : BaseVMActivity<InsureApplyViewModel, ActivityInsureApplyBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<InsureApplyActivity>()
        }
    }

    private val surplus by lazy {
        intent.getIntExtra("surplus", 0)
    }

    private val data = arrayListOf(InsureApplyBean())
    private val adapter = InsureApplyAdapter(data)

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply {
            titleBar.title = "保险申请"
            titleBar.leftImageView().click { finish() }

            recyclerView.vertical()
                .adapter = adapter

            adapter.setOnItemChildClick(id = R.id.ivAddMinus) { view, position, item ->
                // 添加或删除
                if (position == 0) {
                    if (surplus > data.size) {
                        // 可以添加
                        data.add(InsureApplyBean())
                        adapter.notifyDataSetChanged()
                    } else {
                        // 不能添加
                        toast("添加人员已达上限")
                    }
                } else {
                    // 删除 当前索引下的
                    data.removeAt(position)
                    adapter.notifyDataSetChanged()
                }
            }

            adapter.setOnItemChildClick(id = R.id.tvAccountHelp) { view, position, item ->
                // 账户帮助
                XPopup.Builder(mActivity)
                    .asCustom(
                        ConfirmDialog(
                            context = mActivity,
                            title = "温馨提示",
                            content = "指被保障人注册\n周周家服App所用的手机号",
                            cancelStr = "",
                            confirmStr = "我知道了",
                            isHideCancel = true,
                            onCancel = {},
                            onConfirm = {})
                    )
                    .show()
            }

            tvSubmit.clickWithTrigger {
                // 提交
                Log.e("TAG", data.toJson())
                val list =
                    data.filter { it.account.isEmpty() || it.real_name.isEmpty() || it.mobile.isEmpty() || it.card_front.isEmpty() || it.card_side.isEmpty() }
                if (list.isNotEmpty()) {
                    toast("请填写完整信息")
                    return@clickWithTrigger
                }
                vm.applyMasterInsurance(data.toJson())
            }

        }
    }

    override fun initData() {
    }

    override fun startObserve() {

        vm.applyMasterInsuranceData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                setResult(200, Intent().apply {
                    putExtra("insure_count", this@InsureApplyActivity.data.size)
                })
                finish()
            }
        }

    }

    override fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(true)
            statusBarColor(R.color.white)
        }
    }

}